package com.admin.controller;

import com.admin.entity.Category;
import com.admin.entity.Delivery;
import com.admin.entity.Goods;
import com.admin.service.CategoryService;
import com.admin.entity.Storage;
import com.admin.service.DeliveryService;
import com.admin.service.GoodsService;
import com.admin.service.StorageService;
import com.admin.sys.base.entity.DateUtils;
import com.admin.sys.base.module.extend.web.BaseController;
import com.admin.sys.base.entity.ResultInfo;
import com.admin.sys.utils.chart.ChartUtils;
import com.admin.sys.utils.chart.data.DataBend;
import com.admin.sys.utils.chart.data.DataLine;
import com.admin.sys.utils.chart.data.DataPie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 *【图表统计】页面接口
 */
@RestController
@RequestMapping("/admin/count")
public class CountController extends BaseController {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private StorageService storageService;
    @Autowired
    private DeliveryService deliveryService;

    /**
     * 【类别信息】数据统计
     * @param goods
     * @return
     */
    @RequestMapping("/getCount")
    public ResultInfo getCount(Storage goods) {
        Map<String, Object> data = new HashMap<>();
        List<Storage> list = storageService.getList(goods);
        DataLine line = ChartUtils.createLine();
        DataPie pie = ChartUtils.createPie();
        //数据分组统计
        Map<String, IntSummaryStatistics> collect = list.stream().collect(Collectors.groupingBy(Storage::getGoodsId, Collectors.summarizingInt(Storage::getNumber)));
        for (Map.Entry<String, IntSummaryStatistics> entry : collect.entrySet()) {
            Goods entity = goodsService.get(entry.getKey() + "");
            //柱状图
            String name = entity == null ? "未分类" : entity.getName();
            line.getxData().add(name);
            line.getyData().add(entry.getValue().getSum() + "");
            //饼状图
            DataPie.Item item = pie.createItem();
            item.setName(name);
            item.setValue(entry.getValue().getSum() + "");
            pie.getData().add(item);
        }

        //折线图
        List<Delivery> list2 = deliveryService.getList(null);
        DataBend bend = ChartUtils.createBend();
        Map<Date, List<Delivery>> collect1 = list2.stream().collect(Collectors.groupingBy(Delivery::getDeliveryTime));
        LinkedHashMap<Date, List<Delivery>> collectSort = collect1.entrySet().stream().sorted(Map.Entry.comparingByKey()) //按key排序
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> newValue, LinkedHashMap::new));
        //降序: .sorted(Collections.reverseOrder(Map.Entry.comparingByKey()))
        for (Map.Entry<Date, List<Delivery>> entry : collectSort.entrySet()) {
            bend.getxData().add(DateUtils.formatDate(entry.getKey()));
            bend.getyData().add(entry.getValue().size() + "");
        }
        data.put("bend", bend);
        data.put("line", line);
        data.put("pie", pie);
        return ResultInfo.ok("获取列表成功", data);
    }




}



